﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI;

namespace WebStickyNotes.Helpers
{
    public class MegaSelectList
    {
        public MegaSelectList(IEnumerable items, string dataValueField, string dataGroupField, string dataTextField, object selectedValue)
        {
            Items = new List<MegaSelectListItem>();

            foreach (var item in items)
            {
                Items.Add(new MegaSelectListItem
                              {
                                  Value = Eval(item, dataValueField),
                                  GroupName = Eval(item, dataGroupField),
                                  Text = Eval(item, dataTextField),
                                  Selected = Eval(item, dataValueField) == selectedValue.ToString()
                              });
            }
        }

        private static string Eval(object container, string expression)
        {
            var value = container;
            if (!string.IsNullOrEmpty(expression))
            {
                value = DataBinder.Eval(container, expression);
            }
            return Convert.ToString(value, CultureInfo.CurrentCulture);
        }


        public List<MegaSelectListItem> Items { get; set; }
    }
}